package com.fenghuajueli.module_user.activity.register;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.widget.VerificationCodeInput;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.constants.TypeConstant;
import com.fenghuajueli.module_user.databinding.ActivityRegisterOrLoginBinding;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_user.model.UserViewModel;

/**
 * 验证码发送界面
 */
@Route(path = UserModuleRoute.USER_SEND_CODE_ACTIVITY)
public class SendCodeActivity extends UserViewModelBaseActivity implements View.OnClickListener {


    @Autowired(name = "phone")
    String phone;
    @Autowired(name = "type")
    String type;

    private String code = "";

    ActivityRegisterOrLoginBinding binding;

    private UserViewModel userViewModel = new UserViewModel();

    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRegisterOrLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("手机号不能为空！");
            finish();
            return;
        }
        if (TextUtils.isEmpty(type)) {
            ToastUtils.showShort("没有对应类型！");
            finish();
            return;
        }
        initView();
        userViewModel.verificationCodeTime.observe(this, new androidx.lifecycle.Observer<Integer>() {
            @Override
            public void onChanged(Integer time) {
                binding.btnGetCode.setText(time + "s");
                binding.btnGetCode.setEnabled(false);
                if (time == 0) {
                    binding.btnGetCode.setEnabled(true);
                    binding.btnGetCode.setText("获取验证码");
                }
            }
        });
    }


    private void initView() {
        if (TypeConstant.VerCodeType.REGISTER.name().equals(type)) {
            binding.tvCodeType.setText("立即注册");
            binding.loginTypeTips.setText("请输入收到的6位注册验证码");
        } else if (TypeConstant.VerCodeType.FORGIVE.name().equals(type)) {
            binding.tvCodeType.setText("忘记密码");
            binding.loginTypeTips.setText("请输入6位验证码来重置您的登录密码");
            sendCode();
        } else if (TypeConstant.VerCodeType.LOGIN.name().equals(type)) {
            binding.tvCodeType.setText("立即登录");
            binding.loginTypeTips.setText("请输入6位验证码来进行登录");
        }
        binding.codeView.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
            public void onComplete(String content) {
                code = content;
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showShort("请输入验证码");
                    return;
                }
                if (TypeConstant.VerCodeType.FORGIVE.name().equals(type)) {
                    //进行登
                    ARouter.getInstance().build(UserModuleRoute.USER_PASSWORD_SET_ACTIVITY)
                            .withString("phone", phone)
                            .withString("type", type)
                            .withString("code", code)
                            .navigation();
                }
            }
        });
        binding.btnGetCode.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnGetCode) {
            sendCode();
        } else if (view.getId() == R.id.ivClose) {
            finish();
        }
    }

    private void sendCode() {
        if (userViewModel != null) {
            userViewModel.getVerificationCode(this, phone);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}